package org.jeecg.modules.job.rms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.modules.job.rms.entity.RmsCourse;
import org.jeecg.modules.job.rms.entity.RmsCourseApply;
import org.jeecg.modules.job.rms.mapper.RmsCourseApplyMapper;
import org.jeecg.modules.job.rms.mapper.RmsCourseMapper;
import org.jeecg.modules.job.rms.service.IRmsCourseApplyService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description: 课程报名
 * @Author: jeecg-boot
 * @Date:   2022-12-18
 * @Version: V1.0
 */
@Service
public class RmsCourseApplyServiceImpl extends ServiceImpl<RmsCourseApplyMapper, RmsCourseApply> implements IRmsCourseApplyService {


    @Resource
    private RmsCourseMapper courseMapper;

    @Override
    public boolean add(RmsCourseApply param) {
        //判断是否存在，如果不存在，则新建
        RmsCourseApply result=this.getOne(new QueryWrapper<>(param));
        if (result!=null){
            throw new RuntimeException("已报名，无需重复报名");
        }
        this.save(param);
        //添加报名人数
        RmsCourse course=courseMapper.selectById(param.getCourseId());
        course.setNumber(course.getNumber()+1);
        courseMapper.updateById(course);
        return true;
    }

    @Override
    public IPage<RmsCourseApply> findPageBySpec(Page<RmsCourseApply> page, RmsCourseApply param) {
        IPage pageInfo = this.baseMapper.selectPage(page, new QueryWrapper<>(param));
        List<RmsCourseApply> list=pageInfo.getRecords();
        for (int i=0;i<list.size();i++){
            list.get(i).setCourse(courseMapper.selectOne(new QueryWrapper<>(new RmsCourse().setId(list.get(i).getCourseId()))));
        }
        page.setRecords(list);
        return page;
    }
}
